package org.example.demo1.servlets.LoginPage;


import com.google.gson.Gson;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import org.example.demo1.DAO.UserRoleDao;
import org.example.demo1.Entity.User;
import org.example.demo1.utils.ChineseIdentifierUtils;
import org.example.demo1.utils.MybatisUtils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import static org.example.demo1.utils.ChineseIdentifierUtils.identify;
import static org.example.demo1.utils.InquireTools.*;

@WebServlet("/API/register")
public class RegisterServlet extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String EmailOrPhone = req.getParameter("EmailOrPhone");
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        User user = null;
        user = getUserInformation(username);
        Map<String, Object> data = new HashMap<>();
        if (user != null) {
            data.put("status", "error");
            data.put("code", 401);
            data.put("message", "用户名已存在");
            String json = new Gson().toJson(data);
            // 发送响应
            PrintWriter out = resp.getWriter();
            out.print(json);
            out.flush();
            return;
        }
        ChineseIdentifierUtils.IdentifierType type = identify(EmailOrPhone);
        if (type == ChineseIdentifierUtils.IdentifierType.EMAIL) {
            InsertUser(username, password, EmailOrPhone, null);
        } else if (type == ChineseIdentifierUtils.IdentifierType.PHONE) {
            InsertUser(username, password, null, EmailOrPhone);
        } else {
            // 处理无效的邮箱或电话号码
            data.put("status", "error");
            data.put("code", 402);
            data.put("message", "无效的邮箱或电话号码格式");
            String json = new Gson().toJson(data);
            PrintWriter out = resp.getWriter();
            out.print(json);
            out.flush();
            return;
        }
        
        user = getUserInformation(username);


        if (user != null) {
            int user_id=user.getUser_id();
            SqlSession sqlSession= MybatisUtils.getSqlSession();
            UserRoleDao mapper = sqlSession.getMapper(UserRoleDao.class);
            mapper.insertUserRole(user_id, 1);
            sqlSession.commit();
            data.put("status", "success");
            data.put("code", 200);
            data.put("message", "注册成功");
        } else {
            data.put("status", "error");
            data.put("code", 400);
            data.put("message", "注册失败");
        }
        // 使用Gson或Jackson将对象转为JSON
        String json = new Gson().toJson(data);
        // 发送响应
        PrintWriter out = resp.getWriter();
        out.print(json);
        out.flush();
    }

}